﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcContrib.FluentHtml;

using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.Util.Translation;
using Revis.VClinicNet.App.Internal.Dto.Validation;

namespace Revis.VClinicNet.UI.Internal.Mvc.Models.Forms
{
    public class VisitCreateModel
    {
        public long DoctorId { get; set; }
        public long PatientId { get; set; }
        public string Date { get; set; }
        public int Hour { get; set; }
        public int Minute { get; set; }
        public string PatientName { get; set; }

        public VisitCreateModel()
        {
            Date = String.Empty;
            PatientName = String.Empty;
        }

        public void Validate()
        {
            ValidationException ve = new ValidationException("Błąd walidacji podczas tworzenia wizyty");
            if (String.IsNullOrEmpty(Date) == true) {
                ve.Add(new ValidationError("Date", "Data wizyty nie może być pusta"));
            }
            if (Hour < 0 || Hour >= 23) {
                ve.Add(new ValidationError("Hour", "Godzina powinna być z zakresu od 0 do 23"));
            }
            if (Minute < 0 || Minute >= 59) {
                ve.Add(new ValidationError("Minute", "Minuta powinna być z zakresu od 0 do 59"));
            }
            if (ve.Errors.Count > 0) {
                throw ve;
            }
        }
    }

    public static class VisitCreateForm
    {
        private static readonly string DoctorsListKey = "DoctorsList";

        public static IEnumerable<SelectListItem> DoctorList(
            this VisitCreateModel model, ViewDataDictionary viewData)
        {
            return viewData[DoctorsListKey] as IEnumerable<SelectListItem>;
        }

        public static void SetDoctorList(
            this VisitCreateModel model,
            ViewDataDictionary viewData,
            IList<DoctorInfoDto> doctorsList)
        {
            List<SelectListItem> items = doctorsList.Select(d => {
                return new SelectListItem() {
                    Text = d.Name,
                    Value = d.Id.ToString(),
                    Selected = d.Id == model.DoctorId
                };
            }).ToList();
            viewData[DoctorsListKey] = items;
        }

    }
}